import Vue from 'vue'
import VueRouter from 'vue-router'
import UserTableView from "@/views/UserTableView";
import DeviceTableView from "@/views/DeviceTableView";
import AreaTableView from "@/views/AreaTableView";
import Manage from "@/views/Manage";
import HomeView from "@/views/HomeView";
import Login from "@/views/Login";
import User from "@/views/User";
import ElementUI from "element-ui";
import UserApi from "@/api/userApi";
import Histor from "@/views/History";
import Updevice from "@/views/Updevice";
import Downdevice from "@/views/Downdevice";
import duanxinApi from "@/api/duanxinApi";


Vue.use(VueRouter)
Vue.config.productionTip = false
const routes = [
  {
    path: '/',
    name:'login',
    component: Manage,
    meta: {title: '开始',requireAuth:true },
    redirect:'/login',
    children: [
        {name:'home',
        path: '/home',
        component: HomeView,
            meta: {title: '首页'},
      },
      {
        path: 'user',
        name: 'user',
        component:UserTableView,
        meta: { title: '用户管理',requireAuth:true }
      },
      {
        path: 'device',
        name: 'device',
        component: DeviceTableView,
        meta: { title: '设备管理',requireAuth:true },
        children:[
            {
            name:'up',
              path:'up',
              component:Updevice,
              meta:{title: '上线设备',requireAuth:true}
            },
          {
            name:'down',
            path:'down',
            component:Downdevice,
            meta:{title: '未启设备',requireAuth:true}
          },

        ]

      },
      {
        path: 'area',
        name: 'area',
        component: AreaTableView,
        meta: { title: '场所管理',requireAuth:true }
      },
      {
        path: 'mine',
        component: User,
        meta: {title: '个人信息',requireAuth:true}
      },
      {
        path:'history',
        component: Histor,
        meta:{title:'历史数据',requireAuth:true}
      }
    ]
  },
  {
    path: '/login',
    component: Login
  }, {
    path: '/:catchAll(.*)',
    name: '/404',
    component: () => import('../views/Disappear.vue')
  }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  to.matched.some(record => {

    if (record.meta.requireAuth) {
      let user = JSON.parse(localStorage.getItem('user'))
      if (user === null) {
        next({
          path: '/login',
        })
        ElementUI.Message.error('请登录！')
      } else {
        if(user.password===null)
        {
          UserApi.selectUserByName().then(res => {
            if (res.code === "200") {
              next()
            } else {
              next({
                path: '/login',
              })
              ElementUI.Message.error('请登录！')
            }
          })
        }
        else {
          console.log(user.token)
          duanxinApi.signInverificationCode(user.token).then(res=>
          {
            console.log(res+"////////////////////////+++++++++++++++++++++---------------------")
            if(res)
            {
              console.log("登进去了哦////////////////////////+++++++++++++++++++++------------------")
              next()
            } else {
              next({
                path: '/login',
              })
              ElementUI.Message.error('请登录！')
            }
          })
        }

      }
    } else {
      next() // 确保一定要调用 next()
    }
  })
})
export default router
